package org.retabel.service.render;

import org.retabel.persistence.entity.document.SectionConfiguration;
import org.retabel.persistence.entity.report.RenderingEngineEnum;
import org.retabel.service.render.exportable.ExportableDocumentPart;
import org.retabel.service.render.exportable.FopExportableDocumentPart;
import org.retabel.service.render.exportable.JasperReportsExportableDocumentPart;

import java.net.URI;

/**
 * Specific splitter for {@linkplain ExportableDocumentPart}.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 17:02 21.03.12
 *
 * @author Burt Parkers
 * @version 1.0
 */
public class ExportableDocumentPartSplitter extends DocumentConfigurationSplitter<ExportableDocumentPart> {

    /** The cache size. */
    private int cacheSize;
    /** The xml data. */
    private byte[] xmlData;
    /** The design uri. */
    private URI reportDesignUri;


    /**
     * Constructor.
     *
     * @param cacheSize the cache size.
     * @param reportDesignUri the report design uri.
     * @param xmlData the xml data.
     */
    public ExportableDocumentPartSplitter(int cacheSize, URI reportDesignUri, byte[] xmlData) {
        this.cacheSize = cacheSize;
        this.reportDesignUri = reportDesignUri;
        this.xmlData = xmlData;
    }

    @Override
    public ExportableDocumentPart createNew(SectionConfiguration sectionConfiguration) throws Exception {
        return newExportableDocumentPart(sectionConfiguration, xmlData);
    }

    /**
     * Create a new exportable document part instance by rendering engine
     *
     * @param sectionConfiguration the section configuration the report design and rendering engine is retrieved from
     * @param xmlData              the xml data
     * @return an instance of {@link ExportableDocumentPart}
     * @throws Exception thrown if an exception occurs
     */
    private ExportableDocumentPart newExportableDocumentPart(SectionConfiguration sectionConfiguration, byte[] xmlData)
        throws Exception {
        RenderingEngineEnum renderingEngine = sectionConfiguration.getReportDesign().getReportDesignPackage().getRenderingEngine();

        switch (renderingEngine) {
            case JASPER_REPORTS: {

                JasperReportsExportableDocumentPart exportableDocumentPart = new JasperReportsExportableDocumentPart(
                    sectionConfiguration, reportDesignUri, cacheSize);
                exportableDocumentPart.fill(xmlData);

                return exportableDocumentPart;
            }
            case FOP: {
                FopExportableDocumentPart fopExportableDocumentPart = new FopExportableDocumentPart(sectionConfiguration,
                                                                                                    reportDesignUri);

                fopExportableDocumentPart.fill(xmlData);

                return fopExportableDocumentPart;
            }
        }

        throw new RenderServiceException("not supported rendering engine: " + renderingEngine);
    }
}
